package com.adong.controller;

import com.adong.pojo.User;
import com.adong.pojo.Vo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;


/**
 * @author com.adong
 */
@Controller
@RequestMapping("/user")
public class UserController {
    /**
     * @return 要跳转的视图
     * method : 指定请求方式,如果使用post,页面发来的必须是post请求
     */
    @RequestMapping(value = "/quick", method = RequestMethod.POST) //访问的时http://localhost:8080/user/quick
    public String save() {
        System.out.println("UserController save run");
        return "/success.jsp"; //要是不加/ 访问的是http://localhost:8080/user/success.jsp 加了访问http://localhost:8080/web目录/success.jsp
    }

    /**
     * @return params 表示不能没有参数username,age不能等于10
     * headers : 请求头映射条件
     */
    @RequestMapping(value = "/quick",
            params = {"username", "age!=10"}, headers = {"规定请求头"})
    public String test1() {
        return "/success.jsp";
    }

    @RequestMapping(value = "/quick/{id}", method = RequestMethod.DELETE)
    public String test2(@PathVariable Integer id) {
        return "/success.jsp";
    }


    @RequestMapping("/quick2")
    public ModelAndView save2() {

        ModelAndView view = new ModelAndView();
        //设置模型数据
        view.addObject("userName", "com.adong");
        //设置模型数据
        view.setViewName("/success.jsp");
        return view;

    }


    @RequestMapping("/quick3")
    public ModelAndView save3(ModelAndView modelAndView) {
        //设置模型数据
        modelAndView.addObject("userName", "flynn");
        //设置模型数据
        modelAndView.setViewName("/success.jsp");
        return modelAndView;

    }

    @RequestMapping("/quick4")
    public String save4(Model model) {
        //设置模型数据
        model.addAttribute("userName", "flynnDong");
        return "/success.jsp";

    }

    //不常用
    @RequestMapping("/quick5")
    public String save5(HttpServletRequest request) {
        request.setAttribute("userName", "kangjia");
        return "/success.jsp";

    }

    @RequestMapping("/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().print("hello word");
    }

    @RequestMapping("/quick7")
    @ResponseBody
    public String save7() throws IOException {
        return "hello word2";
    }

    @RequestMapping("/quick8")
    @ResponseBody
    public String save8() throws JsonProcessingException {
        User user = new User("com.adong", 18);
        //使用json的转换工具将对象转换成json格式字符串在返回
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        return json;
    }

    @RequestMapping("/quick9")
    @ResponseBody
    //希望SpringMVC将对象转换成Json字符串
    public User save9() {
        User user = new User("flynn", 20);
        return user;
    }

    //获取基本数据请求
    @RequestMapping("/quick10")
    @ResponseBody
    public void save10(String name, int age) {
        System.out.println(name);
        System.out.println(age);

    }

    //获取对象请求
    @RequestMapping("/quick11")
    @ResponseBody
    public void save11(User user) {
        System.out.println(user);

    }

    //获取数组请求
    @RequestMapping("/quick12")
    @ResponseBody
    public void save12(String[] names) {
        System.out.println(Arrays.asList(names));

    }

    @RequestMapping(value = "/quick20")
    @ResponseBody
    public void save20(@RequestHeader(value = "User-Agent", required = false) String user_agent) throws IOException {
        System.out.println(user_agent);
    }


    //获取集合请求
    @RequestMapping("/quick13")
    @ResponseBody
    public void save13(Vo vo) {
        System.out.println(vo);

    }



    //上传文件
    @RequestMapping(value = "/quick14")
    @ResponseBody
    public void save22(String username, MultipartFile uploadFile) throws IOException {
        System.out.println(username);
        String name = uploadFile.getOriginalFilename();
        uploadFile.transferTo(new File("D:\\demo\\" + name));
    }
}
